//
//  CollectPageViewController.m
//  MoGuRadio
//
//  Created by dlios on 15-6-9.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "CollectPageViewController.h"
#import "SpecialPageViewController.h"
#import "YRSideViewController.h"

// 第三方
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"

// 自定义类
#import "DataBaseHandler.h"
#import "MushRoom.h"
#import "BaseModel.h"

// 自定义cell
#import "AllPageTableViewCell.h"

#define TABLEVIEW_REUSE @"MainTableViewReuseIdentifier"

@interface CollectPageViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *collectTbaleView;
@property (nonatomic, retain) NSMutableArray *collectArr;
@end

@implementation CollectPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"蘑菇收藏";
//    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setupViews];
    
    // 导航栏左侧按钮
    UIImage *leftButtonImage = [UIImage imageNamed:@"liftFind@2x"];
    leftButtonImage = [leftButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:leftButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)] autorelease];
    
}

//创建TableView相关
- (void)setupViews
{
    self.collectTbaleView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    self.collectTbaleView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.0];
    self.collectTbaleView.delegate = self;
    self.collectTbaleView.dataSource = self;
    [self.view addSubview:self.collectTbaleView];
    [_collectTbaleView release];
    
    
    [self.collectTbaleView registerClass:[AllPageTableViewCell class] forCellReuseIdentifier:TABLEVIEW_REUSE];
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // 系统编辑按钮的点击方法
    [super setEditing:editing animated:animated];
    
    // 让当前的tableView可以编辑联系人, 切换编辑状态
    [self.collectTbaleView setEditing:editing animated:animated];
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 判断当前行的编辑状态
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSLog(@"删除");
        
        DataBaseHandler *handler = [DataBaseHandler shareInstance];
        MushRoom *mushRoom = [self.collectArr objectAtIndex:indexPath.row];
        [handler deleteMushRoomWithRid:mushRoom.rid];
        [self.collectArr removeObject:[self.collectArr objectAtIndex:indexPath.row]];
        [self.collectTbaleView reloadData];
        
        
        
        //删除一个cell
        [self.collectTbaleView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return VIEW_HEIGHT / 7;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                  TABLEVIEW_REUSE];

    NSString *imageStr = [[self.collectArr objectAtIndex:indexPath.row] pic];
    [cell.mainImageView setImageWithURL:[NSURL URLWithString:imageStr]];
    
    cell.titleLabel.text = [[self.collectArr objectAtIndex:indexPath.row] titleLabelText];
    cell.desLabel.text = [[self.collectArr objectAtIndex:indexPath.row] desLabelText];
    
    NSString *listenStr = [[self.collectArr objectAtIndex:indexPath.row] listenLabelText];
    cell.listenLabel.text = [BaseModel listenChangeStringValue:listenStr];
    
    NSString *focusStr = [[self.collectArr objectAtIndex:indexPath.row] focusLabelText];
    cell.focusLabel.text = [BaseModel foucsChangeStringValue:focusStr];
    
//    UIImage *addButtonImage = [UIImage imageNamed:@"delete"];
//    addButtonImage = [addButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [cell.addButton setImage:addButtonImage forState:UIControlStateNormal];
//    
//    cell.likeLabel.text = @"删除";
    
    // 获取当前点击的cell
    cell.addButton.tag = indexPath.row;
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SpecialPageViewController *specialVC = [[SpecialPageViewController alloc] init];
    
    specialVC.rid = [[self.collectArr objectAtIndex:indexPath.row] rid];
    
    specialVC.pic = [[self.collectArr objectAtIndex:indexPath.row] pic];
    
    specialVC.hostName = [[self.collectArr objectAtIndex:indexPath.row] hostNameText];
    
    specialVC.foucsNum = [[self.collectArr objectAtIndex:indexPath.row] focusLabelText];
    
    specialVC.listenNum = [[self.collectArr objectAtIndex:indexPath.row] listenLabelText];
    
    specialVC.descRadio = [[self.collectArr objectAtIndex:indexPath.row] desLabelText];

    [self.navigationController pushViewController:specialVC animated:YES];
    
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithRed:0.88 green:0.90 blue:0.738 alpha:0.2];
}

//- (void)deleteButtonAction:(UIButton *)button
//{
//    DataBaseHandler *handler = [DataBaseHandler shareInstance];
//    
//    NSString *rid = [[self.collectArr objectAtIndex:button.tag] rid];
//    
//    [handler deleteMushRoomWithRid:rid];
//    
//    
//    
//}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.collectArr = [[DataBaseHandler shareInstance] selectAll];
    [self.collectTbaleView reloadData];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.097 green:0.6701 blue:0.4134 alpha:1.0];
    self.tabBarItem.title = @"收藏";
}


- (void)leftButtonAction:(UIBarButtonItem *)barButton
{
    YRSideViewController *ZHAIRUI = (YRSideViewController *)self.view.window.rootViewController;
    [ZHAIRUI showLeftViewController:YES];
}

- (void)dealloc
{
    [_collectTbaleView release];
    [_collectArr release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
